const connection = require("../app/database");

class authService {
  async create(user) {
    const {name, password} = user;
    console.log(name, password);
    const sql = `INSERT INTO users (name, password) VALUES (?,?)`;
    const result = await connection.execute(sql, [name, password]);
    console.log(result);
    return result[0];
  }

  async getUserByName(name) {
    try {
      const sql = `SELECT * FROM users WHERE name = ?`;
      const result = await connection.execute(sql, [name]);
      return result[0];
    } catch (e) {
      console.log(e);
    }
  }

  async verifyPermission(tableName,resourceId  ,userId) {
    try {
      const sql = `SELECT * FROM ${tableName} WHERE id = ? AND user_id = ?`;
      const result = await connection.execute(sql, [resourceId, userId]);
      return result[0].length > 0;
    } catch (error) {
      console.log(error);
    }
  }
}

module.exports = new authService();